<?php
/**
 * 系統名稱: Lotto 系統
 * 檔案說明: 專案管理-修改
 * $Author$
 * $Id$
 *
 */
set_time_limit(0);
$strWeb = $_SERVER[argv][1];
$_SERVER["DOCUMENT_ROOT"] = "/home/share/www/".$strWeb."/secure";

$_Email = "pinus@uuss.net";
$_Subject = " Compare Supple Wager Error!! (".$strWeb.")";

$intT = time();
$isError = false;
$strLogError = $strWeb."  Time: ".date("Y-m-d H:i:s")."\n===============================================\n";
$_DBSite = "Master";
$_isDBExist = "no";

if(!file_exists($_SERVER["DOCUMENT_ROOT"]."/config.inc.php"))exit($strLogError." no config.inc.php\n\n");

include("/home/share/www/SourceSite-utf8/secure/config.inc.php");
include(__Root_Path."/config.inc.php");
include(__DBConnect_File);
if($_strLogErr != ""){
	$strContent = $strLogError.$_strLogErr;
	mail($_Email, $_Subject, $strContent, "From:".$strWeb."<apache@secure.".$strWeb.">");
	exit($strLogError.$_strLogErr);
}
if(!preg_match("/utf8/i", $_blDBName))exit($strLogError." no UTF8 DB\n\n");

include(__Modules_Path."/Gamble.lib.php");
include(__Modules_Path."/Wager.lib.php");
include(__Common_Path."/SysParam.inc.php");
$strToday = date("Y-m-d");

//找出補貨且未處理注單
$strSQL = "select ID from `bet_BetListTmp` where WagerType = 1 and isPayOff = -2 and PayupDate = '".$strToday."'";
if(!$RS = PMA_mysql_query($strSQL, $_ConnMaster)){
	$strLogError .= $GLOBALS["errormessage"]."\n";
	$strLogError .= "\n Error: ".PMA_mysql_error($_ConnMaster).",\nSQL: ".$strSQL.",\nLine: ".__LINE__.",\nPage: ".__FILE__;
	exit($strLogError."\n\n");
}
if(PMA_mysql_num_rows($RS) == 0)exit($strLogError."\nNo Supple Data\n\n");

$arrWID = array();
while($Rows = PMA_mysql_fetch_object($RS)){
	$arrWID[$Rows -> ID] = $Rows -> ID;
}

//找出補貨外調出去的結果
$ch = curl_init ();
curl_setopt($ch,CURLOPT_URL, "http://".$_KSysParam[SuppleIP]."/k_admin/Supplement/index.php?op=sendSuppleWagerList&suppleid=".join(",", $arrWID)."&acc=".$_KSysParam[SuppleAccount]."&payupdate=".$strToday);
//curl_setopt ($ch, CURLOPT_VERBOSE, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_TIMEOUT, 30);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt ($ch, CURLOPT_HTTPHEADER, array(
				"Accept:image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/x-shockwave-flash, */*",
				"User-Agent:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)",
				"Host:".$_KSysParam[SuppleUrl],
				"Connection:Keep-Alive"));
$strCont = curl_exec ($ch);
curl_close ($ch);
if(!preg_match("/arrWager/", $strCont))exit($strLogError."\nReback No Data\nCont:".$strCont."\n\n");

eval($strCont);

$arrBID = array();
//外調出去沒結果或取消則不計
foreach($arrWID as $intWID){
	if(!is_array($arrWager[$intWID]) || $arrWager[$intWID]["isPayOff"] < 0)continue;
	$arrBID[$intWID] = $intWID;
}
if(sizeof($arrBID) == 0)exit($strLogError."\nReback No BID\n\n");

//若外調出去成功，則將該筆補貨注單改為接收
$strBID = join(", ", $arrBID);
$strLogError .= "--WID:".$strBID."\n";

$strSearchQ = " and isPayOff < 0 ";
$intCType = 0;
$intMulti = 1;
foreach($arrBID as $intWID){
	$strSQL = "update `bet_BetList` set isPayOff = 0 where ID  = ".$intWID;
	$strSQLTmp = "update `bet_BetListTmp` set isPayOff = 0 where ID = ".$intWID;
	$strSQLPNE = "update `bet_ParlaysNumberEven` set isPayOff = 0 where BetListID = ".$intWID;

	PMA_mysql_query("BEGIN", $_ConnMaster);
	//找出外調補貨，尚未接收注單
	$strSQLQ = "select LottoID, Bet_Type, GambleID, Number, MemStake as tolStake, MemPayOff as tolPayOff, MemStake*MemExchangeRate*DirectorRate*DirectorCommission as tolCommission, MemStake*MemExchangeRate*DirectorRate as tolRealStake, MemPayOff*MemExchangeRate*DirectorRate as tolRealPayOff from `bet_BetListTmp` where ID  = ".$intWID." and WagerType = 1 ".$strSearchQ." ";
	if(!$RS = PMA_mysql_query($strSQLQ, $_ConnMaster)){
		$strLogError .= "System Error!!";
		$strLogError .= "\n Error: ".PMA_mysql_error($_ConnMaster).",\nSQL: ".$strSQLQ.",\nLine: ".__LINE__.",\nPage: ".__FILE__."\n\n";
		PMA_mysql_query("ROLLBACK", $_ConnMaster);
		continue;
	}

	if(PMA_mysql_num_rows($RS) == 0)continue;
	$Rows = PMA_mysql_fetch_object($RS);

	//統計金額
	$strOhterCase = " tolSuppleWager = ".$intMulti*$Rows -> tolWager." + tolSuppleWager, tolSuppleStake = tolSuppleStake + ".$intMulti*abs($Rows -> tolRealStake).", tolSupplePayOff = tolSupplePayOff + ".$intMulti*abs($Rows -> tolRealPayOff).", tolSuppleCommission = tolSuppleCommission + ".$intMulti*abs($Rows -> tolCommission);

	//檢查注單是否在排序中
	$intSWID = getWagerOrder($intWID);
	if($intSWID === false){
		$strLogError .= $GLOBALS["errormessage"]."==".$GLOBALS["errorhidden"]."\n\n";
		continue;
	//若注單正在排序中
	}elseif($intSWID > 0){
		$strLogError .= "Wager ID ".$intSWID." is using!".date("Y-m-d H:i:s")."\n\n";
		continue;
	}

	if(!UpdateTodayGameOhterCase($Rows -> GambleID, $Rows -> LottoID, $Rows -> Bet_Type, $Rows -> Number, $strOhterCase)){
		$strLogError .= $GLOBALS["errormessage"]."==".$GLOBALS["errorhidden"]."\n\n";
		continue;
	}
	//接收永久注單
	if(!PMA_mysql_query($strSQL, $_ConnMaster)){
		$strLogError .= "System Error!!";
		$strLogError .= "\n Error: ".PMA_mysql_error($_ConnMaster).",\nSQL: ".$strSQL.",\nLine: ".__LINE__.",\nPage: ".__FILE__."\n\n";
		PMA_mysql_query("ROLLBACK", $_ConnMaster);
		continue;
	}
	if(mysql_affected_rows($_ConnMaster) == 0){
		$strLogError .= "System Error!!";
		$strLogError .= "\n Affected Rows = 0,\nSQL: ".$strSQL.",\nLine: ".__LINE__.",\nPage: ".__FILE__."\n\n";
		PMA_mysql_query("ROLLBACK", $_ConnMaster);
		continue;
	}
	//接收暫存注單
	if(!PMA_mysql_query($strSQLTmp, $_ConnMaster)){
		$strLogError .= "System Error!!";
		$strLogError .= "\n Error: ".PMA_mysql_error($_ConnMaster).",\nSQL: ".$strSQLTmp.",\nLine: ".__LINE__.",\nPage: ".__FILE__."\n\n";
		PMA_mysql_query("ROLLBACK", $_ConnMaster);
		continue;
	}
	if(mysql_affected_rows($_ConnMaster) == 0){
		$strLogError .= "System Error!!";
		$strLogError .= "\n Affected Rows = 0,\nSQL: ".$strSQLTmp.",\nLine: ".__LINE__.",\nPage: ".__FILE__."\n\n";
		PMA_mysql_query("ROLLBACK", $_ConnMaster);
		continue;
	}

	if(!PMA_mysql_query($strSQLPNE, $_ConnMaster)){
		$strLogError .= "System Error!!";
		$strLogError .= "\n Error: ".PMA_mysql_error($_ConnMaster).",\nSQL: ".$strSQLPNE.",\nLine: ".__LINE__.",\nPage: ".__FILE__."\n\n";
		PMA_mysql_query("ROLLBACK", $_ConnMaster);
		continue;
	}
	PMA_mysql_query("COMMIT", $_ConnMaster);

	//寫入記錄
	$strSQL = "insert into `bet_ChangeWagerStatusLog` (UserID, WagerID, CType, IP, AddTime) values('-1', '".$intWID."', '".$intCType."', '".$_SERVER["REMOTE_ADDR"]."', Now())";
	if(!PMA_mysql_query($strSQL, $_ConnMaster)){
		$strLogError .= "System Error!!";
		$strLogError .= "\n Error: ".PMA_mysql_error($_ConnMaster).",\nSQL: ".$strSQL.",\nLine: ".__LINE__.",\nPage: ".__FILE__."\n\n";
		continue;
	}
}  //for($arrBID as $intWID){

$strLogError .= "time:".(time() - $intT);
print $strLogError."\n\n\n";
?>